package com.goldplusgold.tradedelay.app.home.live;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aliyun.vodplayer.media.AliyunLocalSource;
import com.aliyun.vodplayer.media.AliyunVodPlayer;
import com.aliyun.vodplayer.media.IAliyunVodPlayer;
import com.aliyun.vodplayerview.utils.ScreenUtils;
import com.aliyun.vodplayerview.widget.AliyunScreenMode;

import com.bumptech.glide.Glide;
import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.result.ResultLivePlayAddress;
import com.goldplusgold.tradedelay.app.home.live.base.BaseCallback;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.widget.AliyunVodPlayerView;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/17.
 */

public class LiveDetailUpControler {
    private int liveStatus ;
    private String mLiveID ;
    private Context context;
    private SurfaceHolder surfaceHolder = null;
    private String title;
    private String url;
    private String backImg;
    private AliyunVodPlayerView mAliyunVodPlayerView;
    RelativeLayout live_detail_layout_sufaceview;

    TextView titleView;
    ImageView live_sufaceview_backgroundImg;
    RelativeLayout live_sufaceview_titlebar;
    TextView live_sufaceview_control;
    private ImageView mIvBackImageView;
    TextView mLiveNameTextView ;
    int type;
    public LiveDetailUpControler(Context context, RelativeLayout live_detail_layout_sufaceview, int type , String liveID , int liveStatus){
        this.context = context;
        this.live_detail_layout_sufaceview = live_detail_layout_sufaceview;
        this.type = type;
        this.mLiveID = liveID ;
        this.liveStatus = liveStatus ;
    }

    public void setData(String title,String url,String backImg){
        this.title = title;
        this.url = url;
        this.backImg = backImg;
        initView();
        if(titleView != null){
            titleView.setText(this.title);
        }
        initAction();
    }

    private void initAction() {
        mIvBackImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((LiveDetailActivity)context).finish();
            }
        });
    }

    private void initView(){
        mIvBackImageView = (ImageView) this.live_detail_layout_sufaceview.findViewById(R.id.iv_back);
        mLiveNameTextView = (TextView) this.live_detail_layout_sufaceview.findViewById(R.id.live_name_tv);
        mAliyunVodPlayerView = (AliyunVodPlayerView) this.live_detail_layout_sufaceview.findViewById(R.id.video_view);
        titleView = (TextView) this.live_detail_layout_sufaceview.findViewById(R.id.live_sufaceview_title);
        live_sufaceview_backgroundImg = (ImageView)this.live_detail_layout_sufaceview.findViewById(R.id.live_sufaceview_backgroundImg);
        live_sufaceview_titlebar = (RelativeLayout)this.live_detail_layout_sufaceview.findViewById(R.id.live_sufaceview_titlebar);
        live_sufaceview_control = (TextView)this.live_detail_layout_sufaceview.findViewById(R.id.live_sufaceview_control);
        if(type == LiveDetailActivity.LiveDetailType.TEXT_LIVE){
            this.live_detail_layout_sufaceview.findViewById(R.id.live_sufaceview_back).setVisibility(View.VISIBLE);
            titleView.setVisibility(View.VISIBLE);
            mAliyunVodPlayerView.setVisibility(View.GONE);
            live_sufaceview_backgroundImg.setVisibility(View.GONE);
            live_sufaceview_titlebar.setBackgroundResource(R.color.title_bg);
        } else if(type == LiveDetailActivity.LiveDetailType.LIVE) {
            mAliyunVodPlayerView.setVisibility(View.VISIBLE);
            mLiveNameTextView.setText(title);
            mIvBackImageView.setVisibility(View.VISIBLE);
            live_sufaceview_backgroundImg.setVisibility(View.GONE);
            mAliyunVodPlayerView.setTitleBarCanShow(false);
            mAliyunVodPlayerView.setReplaySourceCallback(new AliyunVodPlayerView.ReplaySourceCallback() {
                @Override
                public void setReplaySource() {
                    initPlay();
                }
            });
            initPlay();

        } else if(type == LiveDetailActivity.LiveDetailType.VIDEO || type == LiveDetailActivity.LiveDetailType.VIDEO_LIVE) {
            mIvBackImageView.setVisibility(View.VISIBLE);
            mLiveNameTextView.setText(title);
            mAliyunVodPlayerView.setVisibility(View.VISIBLE);
            live_sufaceview_backgroundImg.setVisibility(View.VISIBLE);
            mAliyunVodPlayerView.setTitleBarCanShow(false);
            Glide.with(context).load(backImg).asBitmap().into(live_sufaceview_backgroundImg);
            live_sufaceview_control.setVisibility(View.VISIBLE);
            mAliyunVodPlayerView.setControlBarCanShow(false);
            live_sufaceview_control.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    live_sufaceview_control.setVisibility(View.GONE);
                    mAliyunVodPlayerView.setControlBarCanShow(true);
                    play();
                }
            });
            mAliyunVodPlayerView.setReplaySourceCallback(new AliyunVodPlayerView.ReplaySourceCallback() {
                @Override
                public void setReplaySource() {
                    initPlay();
                }
            });
            initPlay();
        }
    }

    private void startPlay() {
        if(mAliyunVodPlayerView != null){
            mAliyunVodPlayerView.start();
        }
    }

    public void stopPlay(){
        if(mAliyunVodPlayerView != null){
            mAliyunVodPlayerView.onStop();
        }
    }

    public void onDestroy(){
        if(mAliyunVodPlayerView != null){
            mAliyunVodPlayerView.onDestroy();
        }
    }



    public void play(){
        if(mAliyunVodPlayerView != null){
            if(type == LiveDetailActivity.LiveDetailType.VIDEO|| type == LiveDetailActivity.LiveDetailType.VIDEO_LIVE){
                live_sufaceview_control.setVisibility(View.GONE);
                live_sufaceview_backgroundImg.setVisibility(View.GONE);
            }
            mAliyunVodPlayerView.start();
        }
    }

    private boolean initPlay() {
        AliyunLocalSource.AliyunLocalSourceBuilder asb = new AliyunLocalSource.AliyunLocalSourceBuilder();
        asb.setSource(url);
        asb.setTitle(title);
        mAliyunVodPlayerView.setLocalSource(asb.build());
        if(type == LiveDetailActivity.LiveDetailType.LIVE) {
            mAliyunVodPlayerView.mTipsView.showLoadingDialog();
        }
        mAliyunVodPlayerView.setOnPreparedListener(new IAliyunVodPlayer.OnPreparedListener() {
            @Override
            public void onPrepared() {
                mAliyunVodPlayerView.mTipsView.hideLoadingDialog();
                if(type == LiveDetailActivity.LiveDetailType.LIVE && liveStatus == 30 ) {
                    play();
                }
            }
        });
        mAliyunVodPlayerView.setOnCompletionListener(new IAliyunVodPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(){
                Log.e("直播的状态" , "直播结束");
            }
        });
        mAliyunVodPlayerView.setOnErrorListener(new IAliyunVodPlayer.OnErrorListener() {
            @Override
            public void onError(int i, int i1, String s) {
                mAliyunVodPlayerView.mTipsView.hideLoadingDialog();
                switch (i){
                    case AliyunVodPlayer.ALIYUN_ERR_ILLEGALSTATUS:
                        Log.e("直播的错误状态"  , "非法的播放器状态");
                        break;
                    case AliyunVodPlayer.ALIYUN_ERR_NO_NETWORK:
                        Log.e("直播的错误状态"  , "没有网络，或者网络状态不好");

                        //todo 重新拉流
                        if(type == LiveDetailActivity.LiveDetailType.LIVE){
                            ReLoadPlayerAddress();
                        }

                        break;
                    case AliyunVodPlayer.ALIYUN_ERR_FUNCTION_DENIED:
                        Log.e("直播的错误状态"  , "授权功能被拒绝，没有经过授权");
                        break;
                    case AliyunVodPlayer.ALIYUN_ERR_UNKNOWN:
                        Log.e("直播的错误状态"  , "未知错误");
                        break;
                    case AliyunVodPlayer.ALIYUN_ERR_NO_INPUTFILE:
                        Log.e("直播的错误状态"  , "无输入文件，没有设置AliyunDataSource");
                        break;
                    case AliyunVodPlayer.ALIYUN_ERR_READ:
                        Log.e("直播的错误状态"  , "源读取失败");
                        break;
                    default:
                        Log.e("直播的错误状态"  , i+"");
                        break;
                }
            }
        });
        return true;
    }

    private void ReLoadPlayerAddress() {
        LVDataManager.getInstance().getLivePlayAddress(mLiveID , new BaseCallback<ResultLivePlayAddress>(context){
            @Override
            public void success(Call<ResultLivePlayAddress> call, Response<ResultLivePlayAddress> response) {
                ResultLivePlayAddress bean = response.body();
                AliyunLocalSource.AliyunLocalSourceBuilder asb = new AliyunLocalSource.AliyunLocalSourceBuilder();
                asb.setSource(bean.getFlv());
                mAliyunVodPlayerView.setLocalSource(asb.build());
            }

            @Override
            public void faile(Call<ResultLivePlayAddress> call, Throwable t) {

            }
        });

    }

    private class VideoPreparedListener implements IAliyunVodPlayer.OnPreparedListener {
        @Override
        public void onPrepared() {
            if (mAliyunVodPlayerView != null) {
                if(type == LiveDetailActivity.LiveDetailType.VIDEO|| type == LiveDetailActivity.LiveDetailType.VIDEO_LIVE){
                    live_sufaceview_control.setVisibility(View.GONE);
                    live_sufaceview_backgroundImg.setVisibility(View.GONE);
                }
            }
        }
    }


    public void onConfigurationChanged(Configuration newConfig) {
        if (mAliyunVodPlayerView != null) {
            int orientation = context.getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {                //转为竖屏了。
                //显示状态栏
                ((Activity)context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                mAliyunVodPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

                //设置view的布局，宽高之类
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = mAliyunVodPlayerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = (int) (ScreenUtils.getWight(context) * 9.0f / 16);
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

                //设置为小屏状态
                mAliyunVodPlayerView.changeScreenMode(AliyunScreenMode.Small);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {                //转到横屏了。
                //隐藏状态栏
                ((Activity)context).getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                mAliyunVodPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
                //设置view的布局，宽高
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = mAliyunVodPlayerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

                //设置为全屏状态
                mAliyunVodPlayerView.changeScreenMode(AliyunScreenMode.Full);
            }

        }
    }


}
